package com.xjtu.platform.answer.answer;

import com.xjtu.platform.common.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

@RestController
@RequestMapping("/answer/answer")
public class AnswerAPI {
    @Resource
    private CommonService commonService;

    @Resource
    private AnswerService answerService;

    public CurrentUser getCurrentUserByToken(String token)
    {
        CurrentUser currentUser = TokenUtils.getUserInfo(token,commonService);
        System.out.println(currentUser);
        return currentUser;
    }

    @GetMapping("/getQues")
    public Result getCurrUser(@RequestHeader("Token") String token){
        CurrentUser currentUser = getCurrentUserByToken(token);
        QuestionDto question_info = answerService.getQuestInfo(currentUser);
        if(question_info!=null)
        {
            return Result.success(question_info);
        }
        return Result.fail(Result.ERR_CODE_BUSINESS,"找不到可以回答/审核的问题，返回上个界面");
    }

    @PostMapping("/submit")
    public Result Submit(@RequestHeader("Token") String token, @RequestBody AnswerDto ans){
        if(ans!=null&&ans.getQuestion_id()!=null&&ans.getQuestion_id()!=""){
            System.out.println(ans.getAns());
            System.out.println(ans.getQuestion_id());
            System.out.println("ans!!!!!");
            String user_id = getCurrentUserByToken(token).getUserId();
            ans.setUser_id(user_id);
            answerService.createAnswer(ans);
        }else {
            //return Result.fail(Result.ERR_CODE_BUSINESS,"验证码错误");
        }
//        boolean ok = registerService.register(dto);
        return Result.success();
    }

    @GetMapping("/nextPic/{filename}")
    public void nextPic(@PathVariable String filename, @RequestParam("request_id")String request_id, HttpServletResponse response) throws IOException {
        //String picPath = answerService.getQuest();
        //读取文件的输入流
        System.out.println("fileNme!!!");
        System.out.println(filename);
        System.out.println(request_id);
        InputStream in = new FileInputStream(Constants.PIC_ROOT_PATH+"/"+request_id+"/"+filename);
        //将文件信息输出到浏览器的输出流
        OutputStream out = response.getOutputStream();

        byte[] b = new byte[10240];//容量为10240字节的缓存区
        int len = -1;//从输入流读取并放入缓存区的数据量

        while((len=in.read(b))!=-1){
            out.write(b,0,len);
        }
        out.flush();
        out.close();
        in.close();
    }

    @PostMapping("/getReferenceList")
    public Result getReferenceList(@RequestHeader("Token") String token,@RequestBody AnswerDto ans){
        List<AnswerDto> referenceList = answerService.getReferenceList(ans.getQuestion_id());
        return Result.success(referenceList);
    }

    @PostMapping("/confirm")
    public Result confirm(@RequestHeader("Token") String token,@RequestBody AnswerDto answer){
        CurrentUser currentUser = getCurrentUserByToken(token);
        answer.setUser_id(currentUser.getUserId());
        System.out.println("confirm api!!");
        System.out.println(answer.getAns());
        answerService.confirm(answer);
        return Result.success();
    }

    @PostMapping("/reject")
    public Result reject(@RequestHeader("Token") String token,@RequestBody AnswerDto answer){
        CurrentUser currentUser = getCurrentUserByToken(token);
        answer.setUser_id(currentUser.getUserId());
        if(answerService.reject(answer))
        {
            return Result.success();
        }
        return Result.fail(500,"审核超时或失败");
    }
}
